function [layer_two,motivations,oos,nvec,ncl]=SIV_layer_two(session,flag)

for j=1:10
    if flag==1
        a=load(['./results/house-',num2str(session),'-holdout-',num2str(j),'-layer-one.mat']);
        b=load(['./holdout samples/house-',num2str(session),'-holdout-',num2str(j),'.mat']);
    else
        a=load(['./results/senate-',num2str(session),'-holdout-',num2str(j),'-layer-one.mat']);
        b=load(['./holdout samples/senate-',num2str(session),'-holdout-',num2str(j),'.mat']);
    end
    
    vtsr=a.R;
    C=corrcoef(vtsr','rows','pairwise');
    d=sin(acos(C)/2);
    A=exp(-d.^2);
    A=A-diag(diag(A));
    A(isnan(A))=0;
    D=diag(1./sqrt(nansum(A)));L=eye(size(A,1))-D*A*D;
    [V,E]=eig(L);
    [~,idx]=sort(diag(E));
    V=V(:,idx);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    clear cl hinds_pred ps
    for i1=1:10
        for j1=1:10
            [cl(i1,j1,:),~,~]=kmeans(V(:,2:i1+1),j1,'Replicates',1000,'emptyaction','drop');
            [layer_two,~,motivations,~]=partition_representation(vtsr,cl(i1,j1,:));
            layer_two=(a.layer_one+layer_two);
            tmp=sign(layer_two);
            hinds_pred(i1,j1,:)=tmp(b.hinds);
            %fprintf('%g %g: %g\n',i1,j1,numel(find(hinds_pred(i1,j1,:)==0))/numel(a.hinds));
            tmp=squeeze(hinds_pred(i1,j1,:));
            tmp1=b.votes_no_holdout(b.hinds);
            t1=sign(tmp)-tmp1;
            ps(i1,j1)=numel(find(t1==0))/numel(b.hinds);
            
        end
    end
    oos=max(ps(:));
    [nvec,ncl]=find(ps==oos);
    oos_all(:,:,j)=oos;
    cl0=squeeze(cl(nvec,ncl,:));
    if flag==1
        save(['./results/house-',num2str(session),'-holdout-',num2str(j),'-layer-two.mat'],'cl0','hinds_pred','ps','nvec','ncl','oos','cl');
    else
        save(['./results/senate-',num2str(session),'-holdout-',num2str(j),'-layer-two.mat'],'cl0','hinds_pred','ps','nvec','ncl','oos','cl');
    end
    
end
oos_allm=mean(oos_all,3);
oos=max(oos_allm(:));
[nvec,ncl]=find(oos_allm==oos);
[cl0,~,~]=kmeans(V(:,2:nvec+1),ncl,'Replicates',1000,'emptyaction','drop');
[layer_two,~,motivations,~]=partition_representation(vtsr,cl0);
end
